package com.jeeplus.test.website.websiteContent.controller;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.tools.resp.R;
import com.jeeplus.test.tools.resp.ZyPage;
import com.jeeplus.test.tools.resp.ZyPageRequest;
import com.jeeplus.test.website.websiteContent.domain.vo.WebsiteContentReqVo;
import com.jeeplus.test.website.websiteContent.domain.vo.WebsiteContentRespVo;
import com.jeeplus.test.website.websiteContent.service.WebsiteContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@Api(tags = "网站管理-栏目")
@RestController
@Slf4j
@RequestMapping("/websiteContent")
public class WebsiteContentController {
    @Autowired
    private WebsiteContentService websiteContentService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<ZyPage<WebsiteContentRespVo>> getContentPage(WebsiteContentReqVo websiteContentReqVo, ZyPageRequest pageRequest) {
        long current = pageRequest.getPageNo();
        long size = pageRequest.getPageSize();
        Page<WebsiteContentRespVo> pageResult = websiteContentService.getContentPage(websiteContentReqVo, current, size);
        List<WebsiteContentRespVo> dataList = pageResult.getRecords();
        long total = pageResult.getTotal();
        ZyPage<WebsiteContentRespVo> retPage = ZyPage.getPage(dataList, pageRequest, total);
        return R.suc(retPage);
    }

    @ApiOperation("通过主键查询")
    @GetMapping("/queryById/{id}")
    public R<WebsiteContentRespVo> queryById(@PathVariable("id") String id) {
        WebsiteContentRespVo archivesInfoNwRespVO = websiteContentService.queryById(id);
        return R.suc(archivesInfoNwRespVO);
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public R addContent(WebsiteContentReqVo websiteContentReqVo) {
        return BooleanUtil.isTrue(websiteContentService.addContent(websiteContentReqVo)) ? R.suc() : R.error();
    }

    @ApiOperation("修改")
    @GetMapping("/update")
    public R updateContent(WebsiteContentReqVo websiteContentReqVo) throws ParseException {
        return BooleanUtil.isTrue(websiteContentService.updateContent(websiteContentReqVo)) ? R.suc() : R.error();
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public R deleteContent(@PathVariable("id") String id) {
        return BooleanUtil.isTrue(websiteContentService.deleteContent(id)) ? R.suc() : R.error();
    }

}
